package com.feisuanyz.service.impl;

import com.feisuanyz.entity.LogoManagementDO;
import com.feisuanyz.entity.UserPermissionDO;
import com.feisuanyz.query.LogoManagementQuery;
import com.feisuanyz.repository.LogoManagementRepository;
import com.feisuanyz.repository.UserPermissionRepository;
import com.feisuanyz.response.RestResult;
import com.feisuanyz.service.LogoManagementService;
import com.feisuanyz.util.ModelMapperUtil;
import java.sql.Timestamp;
import java.time.Instant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class LogoManagementServiceImpl implements LogoManagementService {

    @Autowired
    private LogoManagementRepository logoManagementRepository;

    @Autowired
    private UserPermissionRepository userPermissionRepository;

    @Override
    @Transactional
    public RestResult uploadLogo(Integer userId, LogoManagementQuery query) {
        if (!hasPermission(userId, "editor")) {
            return RestResult.fail("000001", "权限不足");
        }

        if (!logoManagementRepository.existsByEventId(query.getEventId())) {
            return RestResult.fail("000001", "事件不存在");
        }

        LogoManagementDO logoManagementDO = ModelMapperUtil.map(query, LogoManagementDO.class);
        logoManagementDO.setCreateBy(userId);
        logoManagementDO.setCreateTime(Timestamp.from(Instant.now()));
        logoManagementDO.setUpdateBy(userId);
        logoManagementDO.setUpdateTime(Timestamp.from(Instant.now()));

        logoManagementRepository.save(logoManagementDO);
        return RestResult.success("调用成功");
    }

    @Override
    @Transactional
    public RestResult deleteLogo(Integer userId, Integer logoId) {
        if (!hasPermission(userId, "editor")) {
            return RestResult.fail("000001", "权限不足");
        }

        if (!logoManagementRepository.existsByLogoId(logoId)) {
            return RestResult.fail("000001", "标志图片不存在");
        }

        logoManagementRepository.deleteById(logoId);
        return RestResult.success("调用成功");
    }

    @Override
    @Transactional
    public RestResult updateLogoUrl(Integer userId, Integer logoId, String newLogoUrl) {
        if (!hasPermission(userId, "editor")) {
            return RestResult.fail("000001", "权限不足");
        }

        if (!logoManagementRepository.existsByLogoId(logoId)) {
            return RestResult.fail("000001", "标志图片不存在");
        }

        LogoManagementDO logoManagementDO = logoManagementRepository.findById(logoId).orElse(null);
        if (logoManagementDO != null) {
            logoManagementDO.setLogoUrl(newLogoUrl);
            logoManagementDO.setUpdateBy(userId);
            logoManagementDO.setUpdateTime(Timestamp.from(Instant.now()));
            logoManagementRepository.save(logoManagementDO);
        }
        return RestResult.success("调用成功");
    }

    private boolean hasPermission(Integer userId, String requiredLevel) {
        // 这里假设 UserPermissionDO 是一个实体类，包含权限信息
        // 实际中需要根据实际情况调整
        return userPermissionRepository.findByUserIdAndPermissionLevel(userId, requiredLevel).isPresent();
    }
}